/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.wooxes.server;

import org.wooxes.client.AnuncisService;
import org.wooxes.client.Anunci;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author woox
 */
public class AnuncisServiceImpl extends RemoteServiceServlet implements AnuncisService{
    public Vector getAnuncis(){
        ConnexioSQL connexio = new ConnexioSQL();
        Connection conn = connexio.getConnection();
        if (conn == null){
            return null;
        }

        Vector v = new Vector();
        try{
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM anunci order by data desc, hora desc");

            String st;
            while (rs.next()){
                Anunci a = new Anunci();
                a.setId(rs.getInt("id"));
                a.setTitol(rs.getString("titol"));
                a.setText(rs.getString("text"));
                //Data
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date d = format.parse(rs.getString("data")+" "+rs.getString("hora"));
                a.setData(d);
                
                v.add(a);
            }
        rs.close();
        statement.close();
        }
        catch(Exception e){
            System.out.println(e);
        }

        return v;
    }
}
